package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    //部门列表展示
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result showDepartmentList(){
        log.info("查询部门列表");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    //1.2删除部门
    @DeleteMapping
    public  Result deleteDepartment(@RequestParam Integer id){
//        System.out.println("删除部门 ,id =" + id);
        log.info("根据id删除部门, id: {}" , id);
        deptService.deleteById(id);
        return Result.success();
    }

    //1.3 添加部门
    @PostMapping
    public Result addDepartment(@RequestBody Dept dept){
//        System.out.println("新增部门"+dept);
        log.info("新增部门, dept: {}" , dept);
        deptService.addDepartment(dept);
        return Result.success();
    }

    //1.4 根据ID查询

    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id){
//        System.out.println("根据"+id+"查询");
        log.info("根据ID查询, id: {}" , id);
        Dept dept = deptService.queryById(id);
        return  Result.success(dept);
    }

    //1.5 修改部门
    @PutMapping
    public Result modifyDepartment(@RequestBody Dept dept){
//        System.out.println("修改部门");
        log.info("修改部门, dept: {}" , dept);
        deptService.modifyDepartment(dept);
        return Result.success();
    }
}